package onlinebanking.client.gui;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class HyperlinkJLabel extends JLabel implements MouseMotionListener, MouseListener {

	private static final long serialVersionUID = 1L;
	
	private String label;

	public HyperlinkJLabel(String label) {
		super("<html><u>" + label + "</u></html>");
		this.label = label;
		this.setForeground(Color.BLUE);
		this.addMouseMotionListener(this);
		this.addMouseListener(this);
	}

	public void mouseDragged(MouseEvent e) {
	}

	/**
	 * This method is taken from inside the source of JLabel (in the inner
	 * AccessibleJLabel class)
	 */
	private Rectangle getTextRectangle() {
		String text = getText();
		Icon icon = (isEnabled()) ? getIcon() : getDisabledIcon();

		if ((icon == null) && (text == null)) {
			return null;
		}

		Rectangle paintIconR = new Rectangle();
		Rectangle paintTextR = new Rectangle();
		Rectangle paintViewR = new Rectangle();
		Insets paintViewInsets = new Insets(0, 0, 0, 0);

		paintViewInsets = getInsets(paintViewInsets);
		paintViewR.x = paintViewInsets.left;
		paintViewR.y = paintViewInsets.top;
		paintViewR.width = getWidth()
				- (paintViewInsets.left + paintViewInsets.right);
		paintViewR.height = getHeight()
				- (paintViewInsets.top + paintViewInsets.bottom);

		Graphics g = getGraphics();
		if (g == null) {
			return null;
		}
		String clippedText = SwingUtilities.layoutCompoundLabel(
				(JComponent) this, g.getFontMetrics(), text, icon,
				getVerticalAlignment(), getHorizontalAlignment(),
				getVerticalTextPosition(), getHorizontalTextPosition(),
				paintViewR, paintIconR, paintTextR, getIconTextGap());

		return paintTextR;
	}

	@Override
	public void setText(String text) {
		super.setText("<html><u>" + text + "</u></html>");
		this.label = text;
	};
	
	public void mouseMoved(MouseEvent e) {
		if (getTextRectangle().contains(e.getPoint())) {
			this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		} else {
			this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		}
	}

	public void mouseClicked(MouseEvent e) {
		if (getTextRectangle().contains(e.getPoint())) {
			System.out.println(label);
		}
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}
	
	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	// /**
	// * Main method for testing this component :-)
	// */
	// public static void main(String[] args) throws Exception {
	// SwingUtilities.invokeAndWait(new Runnable() {
	// public void run() {
	// JFrame frame = new JFrame("Test Frame");
	// Container cont = frame.getContentPane();
	// cont.setLayout(new BorderLayout());
	// cont.add(new HyperlinkJLabel("http://www.google.com"));
	// frame.pack();
	// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	// frame.setVisible(true);
	// }
	// });
	// }
}
